package com.imm.da.utils

import android.os.Looper
import android.util.Log

/**
 * <p>------------------------------------------------------
 * <p> Copyright (C) 2021 zhangmz, All rights reserved.
 * <p> If there is technical discussion, you can contact zhangmz90@foxmail.com
 * <p>------------------------------------------------------
 * <p> des
 * <p>
 *
 * @author Created by Administrator
 * @date on 2021 2021/11/14 12:42
 */
object LogHelper {

    private const val LINE_UP = "└─────────────────────────────────────────────────────────────────────"

    fun vTag(tag : String, content : Any?){
        tagBody(Log.VERBOSE, tag, content)
    }

    fun v(content : Any?){
        tagBody(Log.VERBOSE, content)
    }

    fun eTag(tag : String, content: Any?) {
        tagBody(Log.ERROR, tag, content)
    }

    fun e(content: Any?){
        tagBody(Log.ERROR, content)
    }

    private fun tagBody(type : Int, tag : String, content: Any?){
        runCatching {
            log(type, tag, Throwable().stackTrace[2], content)
        }
    }

    private fun tagBody(type : Int, content: Any?){
        runCatching {
            val elements = Throwable().stackTrace
            val element = elements[2]
            val className = element.className
            val simpleName = className.subSequence(className.lastIndexOf(".") + 1, className.length).toString()
            log(type, simpleName, element, content)
        }
    }

    private fun body(element: StackTraceElement) : String{
        val sb = StringBuilder()
        val className = element.className
        val simpleName = className.subSequence(className.lastIndexOf(".") + 1, className.length).toString()
        sb.append("[thread:${Looper.myLooper()?.thread?.name}];")
        sb.append("[class:$simpleName];")
        sb.append("[method:${element.methodName}];")
        sb.append("[line:${element.lineNumber}]")
        return sb.toString()
    }

    private fun log(type : Int, tag: String, element: StackTraceElement, content: Any?) {
        Log.println(type, tag, " ${body(element)}")
        Log.println(type, tag, " content: $content")
        Log.println(type, tag, LINE_UP)
    }

}